package com.sys.console.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sys.console.business.SysAuthorityService;
import com.sys.console.pojo.SysAuthorityTree;
import com.sys.console.pojo.Sys_Authority;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;
import com.sys.object.common.page.PageBean;


/**
 * 2014-11-05
 * 
 * @author 陈成军 权限标识 action
 */
@Controller
public class SysAuthorityAction {

	@Resource SysAuthorityService sysAuthorityService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "/sysAuthorityList.do")
	public String sysAuthorityList(PageBean pageBean, Model model, Sys_Authority authority) {

		ObjResultException<String> ret = new ObjResultException<String>();
		try {
			pageBean = sysAuthorityService.queryPageList(authority, pageBean);
			model.addAttribute("pageBean", pageBean);
			model.addAttribute("startRow", pageBean.getStartRow());
			model.addAttribute("entity", authority);
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
			}
			model.addAttribute("ret", ret);
		}
		return "/system/authority/list";
	}

	
	/**
	 * 新增标识 GET
	 */
	@RequestMapping(value = "/addSysAuthority.do", method = RequestMethod.GET)
	public String addSysAuthority(Sys_Authority authority,Model model) {
		Sys_Authority record;
		try {
			record = sysAuthorityService.queryByPrimaryKey(authority.getAid());
			model.addAttribute("authority", record);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/system/authority/add";
	}
	
	/**
	 * 新增标识 GET
	 */
	@RequestMapping(value = "/addSysAuthorityByLayer.do", method = RequestMethod.GET)
	public String addSysAuthorityByLayer(Sys_Authority authority,Model model) {
		Sys_Authority record;
		try {
			record = sysAuthorityService.queryByPrimaryKey(authority.getAid());
			model.addAttribute("authority", record);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/system/authority/addLayer";
	}

	/**
	 * 新增标识 POST
	 */
	@RequestMapping(value="/saveSysAuthority.do",method=RequestMethod.POST)
	public String saveSysAuthority(Sys_Authority authority, Model model) {
		
		ObjResultException<String> ret = new ObjResultException<String>();

		try {
			int success = sysAuthorityService.saveOrUpdate(authority);
			
			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
			} else {
				ret.setResultCode(ObjResultCode.AUTHORITY_ADD_ERROR);
			}
		} catch (Exception e) {

			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}

		return "redirect:/sysAuthorityList.do";
	}
	
	/**
	 * 新增标识 POST
	 */
	@RequestMapping(value="/saveSysAuthorityJson.do",method=RequestMethod.POST)
	@ResponseBody
	public ObjResultJSON<Sys_Authority> saveSysAuthorityJson(@RequestBody Sys_Authority authority, Model model) {
		
		ObjResultJSON<Sys_Authority> ret = new ObjResultJSON<Sys_Authority>();
		try {
			int success = sysAuthorityService.saveOrUpdate(authority);
			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
				ret.setData(authority);
			} else {
				ret.setResultCode(ObjResultCode.AUTHORITY_ADD_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
	}
	
	/**
	 * 删除
	 * @param record
	 * @return
	 */
	@RequestMapping("/deleteSysAuthority.do")
	@ResponseBody
	public ObjResultException<String> deleteSysAuthority(int id) {
		ObjResultException<String> ret = new ObjResultException<String>();
		try {
			int success = sysAuthorityService.delete(id);

			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
			} else {
				ret.setResultCode(ObjResultCode.AUTHORITY_DELETE_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
	}
	
	/**
	 * 新增标识 GET
	 */
	@RequestMapping(value = "/allotMenuAuthority.do", method = RequestMethod.GET)
	public String selectedSysAuthority(String ids,String checkName,Model model) {
		model.addAttribute("ids",ids.replaceAll("_", "#"));
		model.addAttribute("checkName",checkName.replaceAll("_", "#"));
		return "/system/authority/allotMenuAuthority";
	}
	/**
	 * 新增标识 GET
	 */
	@RequestMapping("/allotMenuAuthorityJson.do")
	@ResponseBody
	public List<SysAuthorityTree> allotMenuAuthorityJson() {
		try {
			List<SysAuthorityTree> list = sysAuthorityService.selectByTree();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<SysAuthorityTree>();
	}

}
